function []=check_siteROIs(siteID,Acode)
% checks the alignment of the ROIs of one site across different time points
% siteID: the site ID as in the ExpLog database
% Acode: the anlysis code as entered in ExpLog, default is 0
% GK - 22.04.2013

if nargin<2
    disp('Use: check_siteROIs(siteID,Acode)')
    return
end
adata_dir='\\argon.fmi.ch\keller.g\AData\';
stacks_dir='\\argon.fmi.ch\keller.g\ExpLog\Stacks.xlsx';
rd_dir = '\\KELLER-RIG1-ANA\RawData\';

[xls_num,xls_txt]=xlsread(stacks_dir);

cur_exps=unique(xls_num(xls_num(:,1)==siteID,2));
if isempty(cur_exps)
    disp('Site ID not found in DB')
    return
end

use_exps=zeros(length(cur_exps),1);

if Acode~=0
    for jnd=1:length(cur_exps)
        if str2num(xls_txt{find(xls_num(:,2)==cur_exps(jnd),1,'first')+1,9})==Acode
            use_exps(jnd)=1;
        end
    end
    cur_exps(~logical(use_exps))=[];
end
if isempty(cur_exps)
    disp('Site found, but no experiments with this Adata code')
    return
end

ExpIDs=cur_exps;

animalIDtxt=xls_txt{min(find(xls_num(:,1)==siteID))+1,4};
regionIDtxt=xls_txt{min(find(xls_num(:,1)==siteID))+1,11};

for ind=1:length(ExpIDs)
    stackDates{ind}=xls_txt{min(find(xls_num(:,2)==ExpIDs(ind)))+1,5};
end

disp('--------------------------------------------------------------------')
disp(sprintf('Animal %s  -  site %i  -  %s',animalIDtxt,siteID,regionIDtxt));
disp('--------------------------------------------------------------------')

for ind=1:length(ExpIDs)
    [adata_file,mouse_id,userID]=find_adata_file(ExpIDs(ind),adata_dir);
    if ~isempty(adata_file)
        tmpadata=load([adata_dir userID '\' mouse_id '\' adata_file],'ROIs','template','act_map','fnames');
        if ~iscell(tmpadata.ROIs)
            tmp=tmpadata.ROIs;
            tmpadata=rmfield(tmpadata,'ROIs');
            tmpadata.ROIs=cell(4,1);
            tmpadata.ROIs{1}=tmp;
            tmp=tmpadata.template;
            tmpadata=rmfield(tmpadata,'template');
            tmpadata.template=cell(4,1);
            tmpadata.template{1}=tmp;
            tmp=tmpadata.act_map;
            tmpadata=rmfield(tmpadata,'act_map');
            tmpadata.act_map=cell(4,1);
            tmpadata.act_map{1}=tmp;
        end
        adata(ind)=tmpadata;
        disp(sprintf('t: %i \t expID: %i \t %s \t # ROIs: %i %i %i %i - %i',ind,ExpIDs(ind),stackDates{ind},length(adata(ind).ROIs{1}),length(adata(ind).ROIs{2}),length(adata(ind).ROIs{3}),length(adata(ind).ROIs{4}),isfield(adata(ind).ROIs{1},'activity')))
    else
        disp([num2str(ExpIDs(ind)) ' has not been registerd yet']);
    end
end

try
    nbr_piezo_layers=readini([rd_dir userID '\' mouse_id '\' adata(1).fnames{1}(1:end-3) 'ini'],'piezo.nbrlayers');
catch
    nbr_piezo_layers=4;
    disp(['ATTENTION - could not read nbr of piezo layers from ini file -- ' userID '\' mouse_id])
end

if length(ExpIDs)<3
    num_rows=1;
elseif length(ExpIDs)<9
    num_rows=2;
else
    num_rows=3;
end
num_cols=ceil(length(ExpIDs)/num_rows);


for jnd=1:nbr_piezo_layers
    
    figure;
    set(gcf,'menubar','none')
    set(gcf,'color','k')
    colormap gray
    cnt=0;
    
    for knd=num_rows:-1:1
        for ind=1:num_cols
            cnt=cnt+1;
            try
                axes('position',[1/num_cols*(ind-1) 1/num_rows*(knd-1) 1/num_cols 1/num_rows],'color','k');
                imgout=ROIs2image(adata(cnt).ROIs{jnd},size(adata(cnt).template{jnd}),'template',adata(cnt).template{jnd},'type','perim','singlecolor',[1 0 0]);
                imagesc(imgout);
                if cnt==1
                    text(.1,.125,'Template','units','normalized','color','w','fontweight','bold','Interpreter','none')
                    text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                end
                text(.1,.95,[stackDates{cnt}],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                axis off
            end
        end
    end
    
    
    figure;
    set(gcf,'menubar','none')
    set(gcf,'color','k')
    colormap gray
    cnt=0;
    
    for knd=num_rows:-1:1
        for ind=1:num_cols
            try
                cnt=cnt+1;
                axes('position',[1/num_cols*(ind-1) 1/num_rows*(knd-1) 1/num_cols 1/num_rows],'color','k');
                if length(adata(cnt).ROIs{jnd}) > 1
                    imgout=ROIs2image(adata(cnt).ROIs{jnd},size(adata(cnt).act_map{jnd}),'template',adata(cnt).act_map{jnd},'type','perim','singlecolor',[1 0 0]);
                    imagesc(imgout);
                else
                    imagesc(adata(cnt).act_map{jnd})
                end
                if cnt==1
                    text(.1,.125,['Activity Map'],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                end
                text(.1,.95,[stackDates{cnt}],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                axis off
            end
        end
    end
    if length(adata(1).ROIs{1})>1
        figure;
        set(gcf,'menubar','none')
        set(gcf,'color','k')
        cnt=0;
        cntall=0;
        tmpall=zeros(size(adata(1).act_map{1}));
        for knd=num_rows:-1:1
            for ind=1:num_cols
                try
                    cnt=cnt+1;
                    axes('position',[1/num_cols*(ind-1) 1/num_rows*(knd-1) 1/num_cols 1/num_rows],'color','k');
                    
                    tmp=zeros(size(adata(cnt).act_map{jnd}));
                    
                    for lnd=1:length(adata(cnt).ROIs{jnd})
                        cntall=cntall+1;
                        tmp(adata(cnt).ROIs{jnd}(lnd).indices)=lnd;
                        tmpall(adata(cnt).ROIs{jnd}(lnd).indices)=cnt;
                    end
                    
                    imagesc(tmp);
                    
                    
                    if cnt==1
                        text(.1,.125,['ROIs'],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                        text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    end
                    text(.1,.95,[stackDates{cnt}],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    axis off
                end
            end
        end
        figure;
        set(gcf,'menubar','none')
        axes('position',[0 0 1 1],'color','k');
        imagesc(tmpall)
    end
    
end


